package viettravel.guiandevent.place;

import java.util.ArrayList;

import viettravel.guiandevent.R;
import viettravel.guiandevent.customeview.WidgetContainer;
import android.content.Context;
import android.graphics.Typeface;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

public class NewPlaceExpandableAdapter extends BaseExpandableListAdapter{

	private Context mContext;
	private ArrayList<NewPlaceExpadableHeaderObject> mHeaderList;
	//private NewPlaceExpadableChildObject mChildObject;
	
	public NewPlaceExpandableAdapter(Context _context, ArrayList<NewPlaceExpadableHeaderObject> _headerList){
		mContext = _context;
		mHeaderList = _headerList;
	}
	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		ArrayList<NewPlaceExpadableChildObject> childList = mHeaderList.get(groupPosition).getChildList();
		return childList.get(childPosition);
	}

	@Override
	public long getChildId(int groupProsition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view,
			ViewGroup parent) {
		final NewPlaceExpadableChildObject childObject = (NewPlaceExpadableChildObject)getChild(groupPosition, childPosition);
		if(view == null){
			LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inflater.inflate(R.layout.row_expandable_child, null);
		}
		CheckBox checkBox = (CheckBox)view.findViewById(R.id.newPlaceCheckBox);
		checkBox.setChecked(childObject.getIsChecked());
		checkBox.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				childObject.setIsChecked(!childObject.getIsChecked());		
			}
		});
		Typeface tf = Typeface.createFromAsset(mContext.getAssets(),"fonts/utm_caviar.ttf");
		TextView textView = (TextView)view.findViewById(R.id.newPlaceText);
		textView.setTypeface(tf);
		textView.setTextSize(20);
		textView.setText(childObject.getRowName().trim());
		// TODO Auto-generated method stub
		return view;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		ArrayList<NewPlaceExpadableChildObject> childList = mHeaderList.get(groupPosition).getChildList(); 
		// TODO Auto-generated method stub
		return childList.size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return mHeaderList.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return mHeaderList.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View view, ViewGroup parent) {
		// TODO Auto-generated method stub
		NewPlaceExpadableHeaderObject headerObject = (NewPlaceExpadableHeaderObject)getGroup(groupPosition);
		if(view == null){
			LayoutInflater inf = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inf.inflate(R.layout.row_expandable_header, null);
		}
		WidgetContainer widgetContainer = (WidgetContainer)view.findViewById(R.id.newPlaceHeader);
		widgetContainer.setTitle(headerObject.getRowName(), Gravity.CENTER_HORIZONTAL);
		return view;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}
}
